/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.kademelia.model.rpc;

import java.io.Serializable;

import org.jsonmaker.gwt.client.annotation.Transient;
import org.kad.gwt.web.client.kademelia.model.KadId;

/**
 */
public abstract class KadRpcMessage implements Serializable {

    /**
     * serial uid.
     */
    private static final long serialVersionUID = -3892416250958240740L;
    private KadId id;
    private KadId source;
    private KadId target;


    private long creationDate;
    private KadRpcMesageType type;

    /**
     * 
     */
    public KadRpcMessage() {
    }

    /**
     * @param idIn
     */
    public KadRpcMessage(final KadId sourceIn, final KadRpcMesageType typeIn) {
        this(sourceIn, null, typeIn);
    }

    /**
     * @param idIn
     */
    public KadRpcMessage(final KadId sourceIn, final KadId targetIn, final KadRpcMesageType typeIn) {
        this(KadId.generateOne(), sourceIn, targetIn, typeIn);
    }

    /**
     * @param idIn
     */
    public KadRpcMessage(final KadId idIn, final KadId sourceIn, final KadId targetIn, final KadRpcMesageType typeIn) {
        super();
        this.id = idIn;
        this.type = typeIn;
        this.source = sourceIn;
        this.target = targetIn;
        this.creationDate = System.currentTimeMillis();
    }

    /**
     * @return the id
     */
    @Transient
    public KadId getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(KadId id) {
        this.id = id;
    }

    /**
     * @return the source
     */
    @Transient
    public KadId getSource() {
        return source;
    }

    /**
     * @param sourceIn the source to set
     */
    public void setSource(KadId sourceIn) {
        this.source = sourceIn;
    }

    /**
     * @return the target
     */
    @Transient
    public KadId getTarget() {
        return target;
    }

    /**
     * @return the type
     */
    @Transient
    public KadRpcMesageType getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(KadRpcMesageType typeIn) {
        this.type = typeIn;
    }

    /**
     * @param targetIn the target to set
     */
    public void setTarget(KadId targetIn) {
        this.target = targetIn;
    }

    /**
     * @return the creationDate
     */
    @Transient
    public long getCreationDate() {
        return creationDate;
    }
    /**
     * @param creationDateIn the creationDate to set
     */
    public void setCreationDate(long creationDateIn) {
        this.creationDate = creationDateIn;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        KadRpcMessage other = (KadRpcMessage) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "[" + type + "/" + id + " " + source + " => " + target + "]";
    }
}
